import { Laboratory, User } from "@/datasource/Type";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";

const mock = new MockAdapter(axios);
// 过滤http前缀请求
mock.onAny(/^http/).passThrough();
mock.onAny(/^\/api\//).passThrough();

// 地址，支持JS正则表达式
// 正则表达式中 \，由转义符，\/，替代
// 匹配任意字符，\w+；
// $，结束。避免匹配多个
// 整个表达式，置于/  /之间
// 通用的，将{}占位符的字符串路径，转为正则表达式对象
// 例如，/users/{uid}; /users/{uid}/courses/{hid}
function path(p: string) {
  const reg = p.replace(/{\w+}/g, "(\\w+)").replace(/\//g, "\\/") + "$";
  return new RegExp(reg);
}
// ===================================

export interface ResultVO {
  code: number;
  message?: string;
  data?: any;
}

const resultVO:ResultVO={
  code: 200,
  data: {}
}

mock.onPost("/login").reply(c => {
  // 获取请求数据
  // 此时请求的js对象已转为json字符串。因此需要转换回JS对象
  const data = c.data;
  const { userName, password } = JSON.parse(data);
  if (userName == "admin" && password == "admin") {
    resultVO.code = 200;
    resultVO.data = { role: "admin",user:{nickName:"管理员"}};
    resultVO.message = "";
    return [
      200,
      resultVO,
      {
        token:"aeb0ea37fc2be159b6f224c8fd4543b65089cc0d3e92378b65374c4fa0e5b8ba00c0da41e3b475c9a98c3eb826bff5bc7ee901648f972d44607692bb31c6f11b"
          //"744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
      }
    ];
  }
  if(userName == "1001" && password == "1001"){
    resultVO.code = 200;
    resultVO.data = {  role: "teacher" ,user:{nickName:"王波",id:"1",major:"软件工程",profession:"讲师"}};
    resultVO.message = "";
    return [
      200,
      resultVO,
      {
        token:
          "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
      }
    ];
  }
  resultVO.code = 401;
  resultVO.message = "用户名密码错误";
  return [200, resultVO];
});

mock.onPost("/admin/addTeacher").reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { userName:1002,password:1002 };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});

mock.onPost("/admin/teachers").reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const { nickName,major,profession } = JSON.parse(data);
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    if(nickName==null&&major==null&&profession==null){
      resultVO.code = 200;
      resultVO.data = { teacherList:[
        {
          id: "1",
          nickName: "王波",
          major: "软件工程",
          profession: "讲师",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "2",
          nickName: "单颖",
          major: "软件工程",
          profession: "讲师",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "3",
          nickName: "刘丹",
          major: "软件工程",
          profession: "副教授",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "4",
          nickName: "李莉",
          major: "软件工程",
          profession: "副教授",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
      ] };
      return [200, resultVO];
    }
    else if(nickName=="王波"&&major==null&&profession==null){
      resultVO.code = 200;
      resultVO.data = { teacherList:[
        {
          id: "1",
          nickName: "王波",
          major: "软件工程",
          profession: "讲师",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
      ] };
      return [200, resultVO];
    }
    else if(nickName==null&&major=="软件工程"&&profession==null){
      resultVO.code = 200;
      resultVO.data = { teacherList:[
        {
          id: "1",
          nickName: "王波",
          major: "软件工程",
          profession: "讲师",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "2",
          nickName: "单颖",
          major: "软件工程",
          profession: "讲师",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "3",
          nickName: "刘丹",
          major: "软件工程",
          profession: "副教授",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
        {
          id: "4",
          nickName: "李莉",
          major: "软件工程",
          profession: "副教授",
          createTime: "1996-6-6",
          updateTime: "2000-6-3",
        },
      ] };
      return [200, resultVO];
    }
    else{
      resultVO.code = 200;
      resultVO.data = { teacherList:[
        
      ] };
      return [200, resultVO];
    }
   
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});

mock.onGet(path("/admin/teachers/{tid}")).reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { teacher:{
      id:"1",
      nickName:"王波",
      profession:"讲师",
      major:"软件工程"
    } as User};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});

mock.onPost(path("/admin/teachers/{tid}")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const teacher = JSON.parse(data);
  console.log("传过来的",teacher);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onDelete(path("/admin/teachers/{tid}")).reply(c => {
  const token = c.headers?.token;
  console.log(c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/admin/addLaboratory")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const laboratory = JSON.parse(data);
  console.log("传过来的",laboratory);
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = {};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onGet(path("/admin/laboratories")).reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { laboratoryList:[{
      id: "1",
      name:"905",
      number:50,
      description:"苹果电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: "2",
      name:"906",
      number:40,
      description:"华硕电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: "3",
      name:"907",
      number:50,
      description:"联想电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
  ]};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/admin/laboratories/{lid}")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const laboratory = JSON.parse(data);
  console.log("传过来的",laboratory);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onDelete(path("/admin/laboratories/{lid}")).reply(c => {
  const token = c.headers?.token;
  console.log(c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/admin")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const password = JSON.parse(data);
  console.log("传过来的",password);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/admin/teachers/{tid}/password")).reply(c => {
  const token = c.headers?.token;
  console.log("传过来的",c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/teachers/{tid}")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const password = JSON.parse(data);
  console.log("传过来的",c.url,password);
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/teachers/{tid}/addCourse")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const course = JSON.parse(data);
  console.log("传过来的",c.url,course);
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = {};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onGet(path("/teachers/{tid}/courses")).reply(c => {
  const token = c.headers?.token;
  console.log(c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { courseList:[{
      id: "1",
      name:"java程序设计",
      number:50,
      time:32,
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: "1",
      name:"c++程序设计",
      number:40,
      time:16,
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: "1",
      name:"数据结构",
      number:50,
      time:64,
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
  ]};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/teachers/{tid}/courses/{cid}")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const course= JSON.parse(data);
  console.log("传过来的",c.url,course);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onDelete(path("/teachers/{tid}/courses/{cid}")).reply(c => {
  const token = c.headers?.token;
  console.log(c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onGet(path("/teacher/{tid}/course/{cid}/appointment")).reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { laboratoryList:[{
      id: '1',
      name:"905",
      number:50,
      description:"苹果电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: '2',
      name:"906",
      number:40,
      description:"华硕电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
    {
      id: '3',
      name:"907",
      number:50,
      description:"联想电脑",
      createTime: "1996-6-6",
      updateTime: "2000-6-3",
    },
  ]};
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onGet(path("/lab/{lid}/appointment")).reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { 
      appointmentList: [
        {
            lesson: 1,
            day: 1,
            weeks: [
                4,
                5,
                6,
                7,
                8
            ]
        }
    ]
    };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onPost(path("/teacher/{tid}/course/{cid}/lab/{lid}/appointment")).reply(c => {
  const token = c.headers?.token;
  const data = c.data;
  const appointment= JSON.parse(data);
  console.log("传过来的",c.url,appointment);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onGet(path("/teacher/{tid}/appointment")).reply(c => {
  const token = c.headers?.token;
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { 
      appointmentList: [
        {
            ctId: 1403771091146600450,
            cId:"123",
            lId:"1234",
            courseName: "c2",
            labName: "#9011",
            lesson: 1,
            day: 1,
            weeks: [
                1,
                2,3,
                4,
                5,
                6,
                7,
                8,9,10,11,12,13,14,15,16,17,18
            ]
        }
    ]
    };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});
mock.onDelete(path("/teacher/{tId}/appointment/{ctId}")).reply(c => {
  const token = c.headers?.token;
  console.log(c.url);
  
  if (
    token ==
    "744193c872b677aab12a0ced447882f4cf9fca92a09d428a26ed145ed2ed2eec665c8824ebc353042ba2be136efcb5c6"
  ) {
    resultVO.code = 200;
    resultVO.data = { };
    return [200, resultVO];
  }
  resultVO.code = 403;
  resultVO.message = "无权限";
  return [200, resultVO];
});